﻿using System;
using System.Configuration;

namespace WITD
{
    class Configuration
    {
                
        /// <summary>
        /// Saves the specified attributeValue to the specified key in the app.config appSettings element.
        /// </summary>
        /// <param name="backUpFolderKey">The key in the appSettings element to which to save the attributeValue.</param>
        /// <param name="backUpFolder">The attributeValue to save to the specified key.</param>
        public static void SaveToConfig(String backUpFolderKey, String backUpFolder)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Remove(backUpFolderKey);
            var kvElem = new KeyValueConfigurationElement(backUpFolderKey, backUpFolder);
            config.AppSettings.Settings.Add(kvElem);
            // Save the configuration file.             
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.             
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// Retrieves the saved attributeValue for specified key from the app.config appSettings element.
        /// </summary>
        /// <param name="key">The key from which to retrieve the attributeValue.</param>
        /// <returns>The attributeValue from t he specified key, if it exists. Otherwise an empty string.</returns>
        public static String GetSavedBackupFolder(String key)
        {
            if (System.Configuration.ConfigurationManager.AppSettings.GetValues(key) != null)
            {
                return System.Configuration.ConfigurationManager.AppSettings.Get(key);
            }
            return string.Empty;
        }
    }
}
